﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Globalization;

namespace Utilities
{
    public class Logger
    {
        public DataTable dtCSV = new DataTable();

        public Logger()
        {
            DataColumn dc1 = new DataColumn("Time", typeof(string));
            DataColumn dc2 = new DataColumn("Comments", typeof(string));
            dtCSV.Columns.Add(dc1);
            dtCSV.Columns.Add(dc2);
        }

        public void log(string comments)
        {
            // add data row for reporting
            DataRow dr1 = dtCSV.NewRow();
            dr1[0] = DateTime.Now.ToString();
            dr1[1] = comments;
            dtCSV.Rows.Add(dr1);
            try
            {
                if (Utilities.GlobalFile.LOG_FILENAME.Contains(Utilities.GlobalFolder.REPOSITORY))
                    Utilities.CSVUtil.saveDataTableToCSVFile(dtCSV, Utilities.GlobalFile.LOG_FILENAME);
            }
            catch { }
        }

        public void write2File(string comments, string file)
        {
            DataTable dtItems = new DataTable();
            dtItems.Columns.Add(new DataColumn("Items", typeof(string)));

            //save each item on a row of the table
            string[] list = comments.Split(',');
            for (int i = 0; i < list.Length; i++)
            {
                string strItem = list[i].Trim();
                if (!strItem.Equals(""))
                {
                    DataRow dr1 = dtItems.NewRow();
                    dr1[0] = strItem;
                    dtItems.Rows.Add(dr1);
                }
            }

            //Save the datatable into file
            try
            {
                if (Utilities.GlobalFile.LOG_FILENAME.Contains(Utilities.GlobalFolder.REPOSITORY))
                    Utilities.CSVUtil.saveDataTableToCSVFile(dtItems, file);
            }
            catch { }
        }

        public void endLogger()
        {
            try
            {
                //Add to mail
                Utilities.GlobalObject.MAIL.setAttachment(Utilities.GlobalFile.LOG_FILENAME);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
